package com.linkage.event.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.linkage.common.exception.ServiceException;
import com.linkage.event.contents.EventContents;
import com.linkage.event.pojo.dao.EventAttentionMapper;
import com.linkage.event.pojo.dao.EventDiscussesMapper;
import com.linkage.event.pojo.dao.EventRepliesMapper;
import com.linkage.event.pojo.entity.EventAttention;
import com.linkage.event.pojo.entity.EventDiscusses;
import com.linkage.event.pojo.entity.EventReplies;
import com.linkage.event.pojo.vo.ReplyVo;
import com.linkage.event.pojo.vo.UserLocationVo;
import com.linkage.event.service.ProblemService;
import com.linkage.event.utils.UserInterfUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.xml.bind.ValidationException;
import java.util.Date;
import java.util.List;

@Service
public class ProblemServiceImpl implements ProblemService {

    @Resource
    private EventDiscussesMapper eventDiscussesMapper;
    @Resource
    private EventRepliesMapper eventRepliesMapper;
    @Resource
    private EventAttentionMapper eventAttentionMapper;


    @Override
    public PageInfo queryProblemListByPage(EventDiscusses discusses, Integer page, Integer pageSize,String token)throws Exception {
        if(null == page){
            page = EventContents.DEFAULT_PAGE_NO;
        }
        if(null == pageSize){
            pageSize = EventContents.DEFAULT_PAGE_SIZE;
        }
        PageHelper.startPage(page, pageSize);
        List<EventDiscusses> eventDiscusses = this.eventDiscussesMapper.selectByPage(discusses);
        String userCode = token.split(":")[3];
        //处理返参信息
        for(EventDiscusses problem : eventDiscusses){
            EventAttention attention = eventAttentionMapper.selectByPrimaryKey(EventAttention.builder().problemId(problem.getId()).userId(userCode).build());
            if(ObjectUtil.isNotNull(attention)){
                problem.setAttenionId(attention.getId());
            }
            //回复数
            List<EventReplies> replies =  eventRepliesMapper.queryByDiscussesId(problem.getId());
            if(CollUtil.isNotEmpty(replies)){
                problem.setReplyCount(replies.size());
            }else{
                problem.setReplyCount(0);
            }
        }
        return new PageInfo<EventDiscusses>(eventDiscusses);
    }

    @Override
    public void createDiscusses(EventDiscusses eventDiscusses,String token) {
        eventDiscusses.setId(UUID.fastUUID().toString(true));
        UserLocationVo vo = UserInterfUtils.getUserInfo(eventDiscusses.getDiscussUserId(),token);
        eventDiscusses.setUserAvatar(vo.getUserPhotoUrl());
        eventDiscusses.setUserName(vo.getUserRealName());
        eventDiscusses.setUserLocation(vo.getUserMobile());
        eventDiscusses.setHasReply("0");
        eventDiscusses.setDealStatus("0");
        eventDiscusses.setWatchCount(0);
        eventDiscussesMapper.insertSelective(eventDiscusses);
    }

    @Override
    public void createReply(ReplyVo replyVo,String token) {
        //更新问题表
        if(StrUtil.equals("1",replyVo.getIsOfficial())){
            EventDiscusses discusses = eventDiscussesMapper.selectByPrimaryKey(replyVo.getProblemId());
            discusses.setDealStatus("1");
            discusses.setHasReply("1");
            eventDiscussesMapper.updateByPrimaryKeySelective(discusses);
        }
        UserLocationVo vo = UserInterfUtils.getUserInfo(replyVo.getUserId(),token);
        //创建回复信息
        eventRepliesMapper.insertSelective(EventReplies.builder().discussId(replyVo.getProblemId())
                .id(IdUtil.fastSimpleUUID())
                .createTime(new Date()).replyDesc(replyVo.getReplyContent()).isOfficial(replyVo.getIsOfficial())
                .userAvatar(vo.getUserPhotoUrl()).userName(vo.getUserRealName()).replyUserId(replyVo.getUserId())
                .userLocation(vo.getUserMobile()).enabledStatus("1").remark(replyVo.getRemark()).build());
    }

    @Override
    public void deleteProblem(String prolbemId) {
        eventDiscussesMapper.deleteByPrimaryKey(prolbemId);
        eventRepliesMapper.deleteByPrimaryKey(prolbemId);
    }

    @Override
    public EventDiscusses queryProblemInfo(String problemId) {
        EventDiscusses discusses = eventDiscussesMapper.selectByPrimaryKey(problemId);
        List<EventReplies> replies = eventRepliesMapper.queryByDiscussesId(problemId);
        if(CollUtil.isNotEmpty(replies)){
            discusses.setReply(replies);
        }
        //更新查看次数
        eventDiscussesMapper.updateByPrimaryKeySelective(EventDiscusses.builder().id(problemId).watchCount(discusses.getWatchCount()+1).build());
        return discusses;
    }

    @Override
    public void createAttention(EventAttention eventAttention) {
        EventAttention attention = eventAttentionMapper.selectByPrimaryKey(eventAttention);
        if(ObjectUtil.isNotNull(attention)){
            throw new ServiceException(500,"当前问题已关注");
        }
        eventAttention.setCreateTime(new Date());
        this.eventAttentionMapper.insertSelective(eventAttention);
    }

    @Override
    public void cancelAttention(EventAttention eventAttention) {
        this.eventAttentionMapper.deleteByProblemId(eventAttention);
    }

    @Override
    public List<EventDiscusses> myAttention(String userId){
        return eventAttentionMapper.queryMyAttention(userId);
    }


}
